import awsLambdaFastify from '@fastify/aws-lambda'
import assert from 'node:assert'
import { join } from 'node:path'
import { test } from 'node:test'
import { create } from '../index.js'
import { buildConfig, createFromConfig } from './helper.js'

test('should respond 200 on root endpoint', async t => {
  const app = await createFromConfig(
    t,
    buildConfig({
      server: {
        hostname: '127.0.0.1',
        port: 0,
        logger: { level: 'fatal' },
        healthCheck: {
          enabled: true,
          interval: 2000
        }
      }
    })
  )

  t.after(async () => {
    await app.stop()
  })

  const handler = awsLambdaFastify(app.getApplication())

  {
    // No browser (i.e. curl)
    const evt = {
      version: '2.0',
      httpMethod: 'GET',
      path: '/',
      headers: {},
      cookies: [],
      queryStringParameters: ''
    }

    const ret = await handler(evt)

    assert.strictEqual(
      ret.body,
      JSON.stringify({ message: 'Welcome to Platformatic! Please visit https://docs.platformatic.dev' })
    )
    assert.strictEqual(ret.isBase64Encoded, false)
    assert.ok(ret.headers)
    assert.strictEqual(ret.headers['content-type'], 'application/json; charset=utf-8')
    assert.strictEqual(ret.headers['content-length'], '81')
    assert.ok(ret.headers.date)
    assert.strictEqual(ret.headers.connection, 'keep-alive')
  }

  {
    // browser
    const evt = {
      version: '2.0',
      httpMethod: 'GET',
      path: '/',
      headers: {
        'user-agent':
          'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
      },
      cookies: [],
      queryStringParameters: ''
    }

    const ret = await handler(evt)

    // No browser (i.e. curl)
    assert.strictEqual(ret.isBase64Encoded, false)
    assert.ok(ret.headers)
    assert.strictEqual(ret.headers['content-type'], 'text/html; charset=utf-8')
    assert.ok(ret.headers.date)
    assert.strictEqual(ret.headers.connection, 'keep-alive')
  }
})

test('from a config file on disk', async t => {
  const app = await create(join(import.meta.dirname, './fixtures/hello/warn-log.service.json'))

  t.after(async () => {
    await app.stop()
  })

  await app.init()
  const handler = awsLambdaFastify(app.getApplication())

  // No browser (i.e. curl)
  const evt = {
    version: '2.0',
    httpMethod: 'GET',
    path: '/',
    headers: {},
    cookies: [],
    queryStringParameters: ''
  }

  const ret = await handler(evt)

  assert.strictEqual(ret.body, JSON.stringify({ hello: 'world' }))
  assert.strictEqual(ret.isBase64Encoded, false)
  assert.ok(ret.headers)
  assert.strictEqual(ret.headers['content-type'], 'application/json; charset=utf-8')
  assert.strictEqual(ret.headers['content-length'], '17')
  assert.ok(ret.headers.date)
  assert.strictEqual(ret.headers.connection, 'keep-alive')
})
